$(function(){
	//表单渲染
	$('#form1').submit(function() {
		var options = {
			url : ctp + "/projectController/projectSave.shtml",
			type : 'POST',
			success : function(result) {
				if(result=='1') {  
					jalert9("保存成功！");
					setTimeout(function(){
						parent.searchData();
						back();
					},WARN_TIME_OUT);
				}else if(result=='0') {     
					jalert("保存失败，请联系管理员！");   
				} else if(result=='2'){
					jalert("项目名称已存在，请更换！");
				}
			}
		};
		$('#form1').ajaxSubmit(options);
		return false;
	});
	getContractList(recId);
	
	setTimeout(function(){
		//$("#areaCode").attr("data-code",'"'+areaCode+'"');
		//$("#areaCode").attr("value",areaName);
	},1000);
});

//合同回填
function getContractList(recId){
	$.ajax({ 
	 	url: ctp + "/projectController/getProjectContracts.shtml",
	    data: {recId:recId},
		type : 'POST',
		async:false, 
		dataType: "json",
		success : function(result) {
			var list=result.list;
			var html="";
			for (var i = 0; i < list.length; i++) {
				html += '<div class="smart-layer_carousel justone" id="' + list[i].rec_id + '">'
				+ '<img src="' + ctp + '/' + list[i].path + '" layui-src="' + ctp + '/' + list[i].path + '">'
				+ '<p><a onclick="delContractByid(\'' + list[i].rec_id
				+ '\');">删除</a>'
				+'</div>';
				
				/*<a onclick="downContract(\''
					+ list[i].path + '\',\'' + list[i].name
					+ '\');">下载</a>*/		    
			};
		    $('#contractList').html(html);
		    layui.layer.photos({
				photos : '#contractList',
				anim : 0
			});
		}
	});
}

function delContractByid(recId){
	jconfirm("del('"+recId+"')","确定要删除所选文件吗？");
}

function del(recId){
	$.ajax({ 
	 	url: ctp + "/projectController/projectContractDel.shtml",
	    data: {recId:recId},
		type : 'POST',
		async:false, 
		//dataType: "json",
		success : function(result) {
			if(result==''){
				$('#'+recId).remove();
				jalert9("删除成功！");
			}else{
				jalert(msg);
			}
		}
	});
}

function downContract(path,fileName){
	location.href=ctp + "/pubController/downloadFile.shtml?path="+path+"&fileName="+fileName;
}

function uploadFile(){
	//alert(document.getElementById("contract").files[0].name);
	//alert(document.getElementById("contract").files[1].name);
	//alert(document.getElementById("contract").files[2].name);
	var files=document.getElementById("contract").files;
	var fileStr="";
	for(var i=0;i<files.length;i++){
		fileStr+=files[i].name+'|';
	}
	$('#file-name').html(fileStr);
}

function projectSave(){
	setTagAndValue("form1","areaLName",$('#areaCode').val());
	setTagAndValue("form1","areaCode",$('#areaCode').attr("data-code"));
	if (!Validator.Validate(document.getElementById("form1"), 1)) {
		return;
	}
	$('#form1').submit();
		
}

function back(){
	window.parent.closeDialogWin();
}